<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信号" prop="wxnum">
        <el-input
          v-model="queryParams.wxnum"
          placeholder="请输入微信号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约时间" prop="appointime">
        <el-date-picker clearable
                        v-model="queryParams.appointime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="完成时间" prop="fulfilltime">
        <el-date-picker clearable
                        v-model="queryParams.fulfilltime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="班级" prop="helpclass">
        <el-input
          v-model="queryParams.helpclass"
          placeholder="请输入班级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帮扶类型" prop="helptype">
        <el-select v-model="queryParams.helptype" placeholder="请选择帮扶类型" clearable>
          <el-option
            v-for="dict in dict.type.student_help_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="帮扶状态" prop="helpstate">
        <el-select v-model="queryParams.helpstate" placeholder="请选择帮扶状态" clearable>
          <el-option
            v-for="dict in dict.type.student_help_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="问题描述" prop="problemdp">
        <el-input
          v-model="queryParams.problemdp"
          placeholder="请输入问题描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:help:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:help:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:help:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:help:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="helpList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="顺序号码" align="center" prop="id" />
      <el-table-column label="学生姓名" align="center" prop="name" />
      <el-table-column label="微信号" align="center" prop="wxnum" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="预约时间" align="center" prop="appointime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.appointime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="fulfilltime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fulfilltime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级" align="center" prop="helpclass" />
      <el-table-column label="帮扶类型" align="center" prop="helptype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.student_help_type" :value="scope.row.helptype"/>
        </template>
      </el-table-column>
      <el-table-column label="帮扶老师" align="center" prop="helpteacher.name" />
      <el-table-column label="帮扶状态" align="center" prop="helpstate">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.student_help_status" :value="scope.row.helpstate"/>
        </template>
      </el-table-column>
      <el-table-column label="问题描述" align="center" prop="problemdp" />
      <el-table-column label="老师备注" align="center" prop="teremaek" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:help:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:help:remove']"
          >删除</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="evaluatEhandleAdd(scope.row)"
              v-hasPermi="['system:evaluate:edit']"
            >评价</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加帮扶管理对话框 -->
    <el-dialog title="添加帮扶管理" :visible.sync="addFormVisible" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="微信号" prop="wxnum">
          <el-input v-model="form.wxnum" placeholder="请输入微信号" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="预约时间" prop="appointime">
          <el-date-picker clearable
                          v-model="form.appointime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="班级" prop="helpclass">
          <el-input v-model="form.helpclass" placeholder="请输入班级" />
        </el-form-item>
        <el-form-item label="帮扶老师" prop="helpteacherId">
          <el-select v-model="form.helpteacherId" placeholder="请选择帮扶老师">
            <el-option
              v-for="dict in helpteacher"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帮扶类型" prop="helptype">
          <el-select v-model="form.helptype" placeholder="请选择帮扶类型">
            <el-option
              v-for="dict in dict.type.student_help_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帮扶状态" prop="helpstate">
          <el-select v-model="form.helpstate" placeholder="请选择帮扶状态">
            <el-option
              v-for="dict in dict.type.student_help_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题描述" prop="problemdp">
          <el-input v-model="form.problemdp" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addSubmitForm">确 定</el-button>
        <el-button @click="addCancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改帮扶管理对话框 -->
    <el-dialog title="修改帮扶管理" :visible.sync="editFormVisible" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入学生姓名" disabled="true"/>
        </el-form-item>
        <el-form-item label="微信号" prop="wxnum">
          <el-input v-model="form.wxnum" placeholder="请输入微信号" disabled="true"/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" disabled="true"/>
        </el-form-item>
        <el-form-item label="班级" prop="helpclass">
          <el-input v-model="form.helpclass" placeholder="请输入班级" disabled="true"/>
        </el-form-item>
        <el-form-item label="帮扶老师" prop="helpteacherId">
          <el-select v-model="form.helpteacherId" placeholder="请选择帮扶老师">
            <el-option
              v-for="dict in helpteacher"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帮扶类型" prop="helptype">
          <el-select v-model="form.helptype" placeholder="请选择帮扶类型" disabled="true">
            <el-option
              v-for="dict in dict.type.student_help_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帮扶状态" prop="helpstate">
          <el-select v-model="form.helpstate" placeholder="请选择帮扶状态">
            <el-option
              v-for="dict in dict.type.student_help_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="完成时间" prop="fulfilltime">
          <el-date-picker clearable
                          v-model="form.fulfilltime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="老师备注" prop="teremaek">
          <el-input v-model="form.teremaek" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editSubmitForm">确 定</el-button>
        <el-button @click="editCancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加帮扶评价对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评价分数" prop="grade">
          <el-select v-model="form.grade" placeholder="请选择评价分数">
            <el-option
              v-for="dict in dict.type.student_help_evaluate"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评价内容" prop="appraise">
          <el-input v-model="form.appraise" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHelp, getHelp, delHelp, addHelp, updateHelp } from "@/api/system/help";
import {listTeacher} from "@/api/system/teacher";
import {addEvaluate, getEvaluate, updateEvaluate} from "@/api/system/evaluate";

export default {
  name: "Help",
  dicts: ['student_help_type', 'student_help_status','student_help_evaluate'],
  data() {
    return {
      addFormVisible:false,
      editFormVisible:false,
      //
      helpteacher:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 帮扶管理表格数据
      helpList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        wxnum: null,
        phone: null,
        appointime: null,
        fulfilltime: null,
        helpclass: null,
        helptype: null,
        helpteacherId: null,
        helpstate: null,
        problemdp: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "学生姓名不能为空", trigger: "blur" }
        ],
        appointime: [
          { required: true, message: "预约时间不能为空", trigger: "blur" }
        ],
        fulfilltime: [
          { required: true, message: "完成时间不能为空", trigger: "blur" }
        ],
        helptype: [
          { required: true, message: "帮扶类型不能为空", trigger: "change" }
        ],
        helpstate: [
          { required: true, message: "帮扶状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  /** 帮扶评价表 */
  euHandleUpdate(row) {
    this.reset();
    const id = row.id || this.ids
    getEvaluate(id).then(response => {
      this.form = response.data;
      this.open = true;
      this.title = "帮扶评价";
    });
  },
  methods: {
    /** 查询帮扶管理列表 */
    getList() {
      this.loading = true;
      listHelp(this.queryParams).then(response => {
        this.helpList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    addCancel() {
      this.addFormVisible = false;
      this.reset();
    },
    editCancel() {
      this.editFormVisible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        wxnum: null,
        phone: null,
        appointime: null,
        fulfilltime: null,
        helpclass: null,
        helptype: null,
        helpteacherId: null,
        helpstate: null,
        problemdp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交评价按钮操作 */
    submitForm() {
      this.$refs["form"].validate(valid => {
            addEvaluate(this.form).then(response => {
              console.log(this.from)
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
      });
    },
    /** 评价按钮操作 */
    evaluatEhandleAdd(row) {
      this.reset();
      this.form.helpId = row.id;
      this.open = true;
      this.title = "帮扶评价";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.addFormVisible = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHelp(id).then(response => {
        this.form = response.data;
        this.editFormVisible = true;
      });
    },
    /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         updateHelp(this.form).then(response => {
    //           this.$modal.msgSuccess("修改成功");
    //           this.editFormVisible = false;
    //           this.getList();
    //         });
    //       } else {
    //         addHelp(this.form).then(response => {
    //           this.$modal.msgSuccess("新增成功");
    //           this.addFormVisible = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    editSubmitForm(){
        updateHelp(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.editFormVisible = false;
          this.getList();
        });
    },
    addSubmitForm(){
        addHelp(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.addFormVisible = false;
          this.getList();
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除帮扶管理编号为"' + ids + '"的数据项？').then(function() {
        return delHelp(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/help/export', {
        ...this.queryParams
      }, `help_${new Date().getTime()}.xlsx`)
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 获取帮扶老师 */
    getHelpteacher() {
      listTeacher('helpteacher').then(response => {
        this.helpteacher = response.rows;
      });
    }
  },

  mounted() {
    this.getHelpteacher();
  }
};
</script>
